import { request } from '@/utils/request'
import store from '@/store'
import router from '@/router'
import { localRouter } from './localRouter'
import BlankPage from "@/components/BlankPage";
import RouterView from "@/components/RouterView";
import { openPage } from "@/utils/tabs.js"
const Layout = () => import("@/layout/Layout.vue");
const modules = import.meta.globEager("../pages/**/**.vue");

import { ROUTER_REQUEST } from '@/config/setting.config.js'

/**
 * 请求菜单
 */
export const requestMenus = (success, error) => {
    if(ROUTER_REQUEST) {
        request({
            url: '/menu/get',
            method: 'post',
        }).then((resp)=>{
            success(resp.data);
        }).catch(e => {
            error();
        });
    }else {
        success(localRouter);
    }
    
}

/**
 * 解析路由
 */
export const parseMenus = (menus, basePath) => {
    let routes = [];
    for(let i = 0; i < menus.length; i ++) {

        menus[i].path = (basePath ? basePath : '') + menus[i].url;

        let route = {
            ...menus[i]
        };
        route.label = route.name;
        route.name = route.name + Math.random();
        if(menus[i].pagePath && modules["../" + menus[i].pagePath]) {
            route.component = modules["../" + menus[i].pagePath].default;
        }else if(menus[i].children && menus[i].children.length > 0){
            route.component = RouterView;
        }else {
            route.component = BlankPage;
        }

        if(menus[i].children && menus[i].children.length > 0) {
            let children = parseMenus(menus[i].children, menus[i].path);
            route.children = children;
        }

        routes.push(route);
    }
    return routes
}

export const getFirstMenu = (menus) => {
    if(menus && menus.length > 0) {
        if(menus[0].children && menus[0].children.length > 0) {
            return getFirstMenu(menus[0].children)
        }else {
            return menus[0];
        }
    }
    return null;
}

/**
 * 初始化路由
 */
 export const initRouter = (cb) => {
    let token = store.getters['user/getToken'];
    if(token) {
        requestMenus((menus) => {
            menus = parseMenus(menus);
            store.dispatch('menu/setMenus', menus);
            let layoutRoutes = {
                path: '/',
                name: 'layout',
                component: Layout,
                children: menus
            };
            router.addRoute(layoutRoutes);
            let firstMenu = getFirstMenu(menus);
            store.dispatch('tabs/initTab', firstMenu);
            store.dispatch('menu/setMenuLoadStatus', true);
            if(cb) {
                cb();
            }
        }, error => {
            store.dispatch('menu/setMenuLoadStatus', true);
        });
    } else {
        store.dispatch('menu/setMenuLoadStatus', true);
    }
}

/**
 * 通过path获取陆游
 */
export const getMenuByPath = (menus, path) => {
    for(let i = 0; i < menus.length; i ++) {
        if(menus[i].path == path) {
            return menus[i];
        }
        
        if(menus[i].children && menus[i].children.length > 0) {
            let menu = getMenuByPath(menus[i].children, path);
            if(menu) {
                return menu;
            }
        }
    }
    return null;
}